package demo1;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author lishunlin110
 * @version 1.0
 * @description: JDBC工具类
 * @date 2021/8/21 16:36
 */
public class JDBCUtils {

    private static final Properties PROPERTIES = new Properties();

    static {
        InputStream is = JDBCUtils.class.getResourceAsStream("jdbc.properties");
        try {
            PROPERTIES.load(is);
            Class.forName(PROPERTIES.getProperty("driverClass"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        Connection con = null;
        String url=PROPERTIES.getProperty("url");
        String user=PROPERTIES.getProperty("user");
        String password=PROPERTIES.getProperty("password");
        try {
            con = DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    public static void closeAll(Connection con, Statement st, ResultSet rs){

        try {
            if(rs!=null){
                rs.close();
            }
            if(st!=null){
                st.close();
            }
            if(con!=null){
                con.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
